﻿namespace Leetcode.N0209;

public class Solution
{
    public int MinSubArrayLen(int target, int[] nums)
    {
        int minLength = int.MaxValue;
        int indexTo = 0;
        int sum = 0;
        int indexFrom = 0;
        while (true)
        {
            for (; indexTo < nums.Length && sum < target; indexTo++)
            {
                sum += nums[indexTo];
            }

            if (sum < target || minLength == 1)
            {
                return minLength == int.MaxValue ? 0 : minLength;
            }

            while (sum >= target)
            {
                minLength = Math.Min(minLength, indexTo - indexFrom);
                sum -= nums[indexFrom];
                indexFrom++;
            }
        }
    }
}
